package io.gitee.zicai.bus.sample.controller;

import io.gitee.zicai.bus.sample.adaptor.CmdReceiveAdaptor;
import io.gitee.zicai.bus.sample.adaptor.CmdSendAdaptor;
import io.gitee.zicai.bus.sample.cmd.CmdEnum;
import io.gitee.zicai.bus.sample.cmd.CmdReceiveData;
import io.gitee.zicai.bus.sample.cmd.CmdSendParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * CmdController
 *
 * @author zicai
 * @since 2021-04-02
 */
@Api(tags = "cmd相关接口")
@RestController
@RequestMapping("/cmd")
public class CmdController {

    @Autowired
    private CmdSendAdaptor cmdSendAdaptor;
    @Autowired
    private CmdReceiveAdaptor cmdReceiveAdaptor;

    @GetMapping("/send/{cmd}/{message}")
    public String send(@ApiParam(value = "cmd(hello,other)") @PathVariable String cmd,
                       @ApiParam(value = "message payload") @PathVariable String message) {
        cmdSendAdaptor.send(new CmdSendParam().setCmdEnum(CmdEnum.parse(cmd)).setMessage(message));
        return "success";
    }

    @GetMapping("/receive/{cmd}/{data}")
    public String receive(@ApiParam(value = "cmd(hello,other)") @PathVariable String cmd,
                          @ApiParam(value = "data payload") @PathVariable String data) {
        cmdReceiveAdaptor.receive(new CmdReceiveData().setCmdEnum(CmdEnum.parse(cmd)).setData(data));
        return "success";
    }
}
